#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>
#include <QPainter>
#include <QApplication>
#include <QMainWindow>
#include <QStyleOptionTitleBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置图片
    QPixmap *pix = new QPixmap(":/0a11ebeb571a63e7c3e15dcfbf78dbc3.jpg");
    QSize sz = ui->label_login_image->size();
    ui->label_login_image->setPixmap(pix->scaled(sz));
    //阴影
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(-3, 0);
    shadow->setColor(QColor("#888888"));
    shadow->setBlurRadius(30);
    ui->label_login_image->setGraphicsEffect(shadow);

    //去窗口边框
    this->setWindowFlags(Qt::FramelessWindowHint);
    //关闭按钮
    QIcon icon1;
    icon1.addFile(":/l131r6W5qb.png");//添加图形，将加载进来的资源进行保存
    ui->pushButton_3->setIcon(icon1);
    ui->pushButton_3->setIconSize(QSize(30,30));
    connect(ui->pushButton_3,&QPushButton::clicked,this,&QWidget::close); //方式一

}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    //只允许左键拖动   持续的动作
    if(event->buttons()&Qt::LeftButton)  //buttons处理的是长事件，button处理的是短暂的事件
    {
        //窗口跟随鼠标移动
        //窗口的新位置=鼠标当前位置-差值
        move(event->globalPos()-m_pt);

    }
}

void MainWindow::mousePressEvent(QMouseEvent *ev)
{
    // 如果鼠标左键按下  单击
    if(ev->button()&Qt::LeftButton)
    {
        //求差值=鼠标当前位置-窗口左上角点
        m_pt=ev->globalPos()-this->geometry().topLeft();  //geometry()是矩形窗口，topLeft()是左上角的信息。
    }
}


MainWindow::~MainWindow()
{
    delete ui;
}

/*
void sqlite_Init()
{

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("user.db");
    if(!db.open())
    {
        qDebug()<<"open error";
    }
    //create excle
    QString createsql=QString("create table if not exists user(id integer primary key autoincrement,"
                        "username ntext unique not NULL,"
                        "password ntext not NULL)");
    QSqlQuery query;
    if(!query.exec(createsql)){
        qDebug()<<"table create error";
    }
    else{
        qDebug()<<"table create success";
    }
}
void MainWindow::on_btn_signin_clicked()
{
    sqlite_Init();
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();
    QString sql=QString("select * from user where username='%1' and password='%2'")
            .arg(username).arg(password);
    //创建执行语句对象
    QSqlQuery query(sql);
    //判断执行结果
    if(!query.next())
    {
        qDebug()<<"Login error";
        QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");

    }
    else
    {
        qDebug()<<"Login success";
        QMessageBox::information(this,"登录认证","登录成功");
        QWidget *w = new QWidget;
        w->show();
        this->close();
    }
}
*/
